/**
 * @file board_ui.cpp
 * @author fwy (2643660853@qq.com)
 * @brief
 * @version 0.1
 * @date 2025-08-18
 *
 * @copyright Copyright (c) 2025
 *
 */

#include "board.h"
#include <QMessageBox>
#include "info_dialog.h"
#include "cabinet_dialog.h"
#include "admin_dialog.h"

void setupMainUI(Board *board, QWidget *window)
{
    QVBoxLayout *mainLayout = new QVBoxLayout(window);

    // 顶部布局不变
    QHBoxLayout *topLayout = new QHBoxLayout();
    QLabel *title = new QLabel("欢迎使用", window);
    title->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    QPushButton *adminBtn = new QPushButton("管理员登录", window);
    topLayout->addWidget(title);
    topLayout->addStretch();
    topLayout->addWidget(adminBtn);
    mainLayout->addLayout(topLayout);

    // 主体区域：左右分栏
    QHBoxLayout *bodyLayout = new QHBoxLayout();

    // 左侧按钮区（居中排列）
    QVBoxLayout *btnLayout = new QVBoxLayout();
    btnLayout->addStretch(); // 顶部弹性空间

    QPushButton *storeBtn = new QPushButton("领柜存包", window);
    QPushButton *pickupBtn = new QPushButton("开柜取物", window);
    QPushButton *releaseBtn = new QPushButton("离场退柜", window);
    QPushButton *longRentBtn = new QPushButton("长租柜", window); // 新增按钮

    QString btnStyle = "QPushButton {"
                       "font-size: 32px;"
                       "font-weight: bold;"
                       "background-color: #01ddfd;"
                       "color: white;"
                       "border-radius: 16px;"
                       "padding: 24px 48px;"
                       "margin: 24px;"
                       "min-height: 120px;"
                       "}"
                       "QPushButton:hover {"
                       "background-color: #0099cc;"
                       "}";
    storeBtn->setStyleSheet(btnStyle);
    pickupBtn->setStyleSheet(btnStyle);
    releaseBtn->setStyleSheet(btnStyle);
    longRentBtn->setStyleSheet(btnStyle);

    btnLayout->addWidget(storeBtn);
    btnLayout->addSpacing(40);
    btnLayout->addWidget(pickupBtn);
    btnLayout->addSpacing(40);
    btnLayout->addWidget(releaseBtn);
    btnLayout->addSpacing(40);
    btnLayout->addWidget(longRentBtn);

    btnLayout->addStretch(); // 底部弹性空间

    // 分隔线
    QFrame *line = new QFrame(window);
    line->setFrameShape(QFrame::VLine);
    line->setFrameShadow(QFrame::Sunken);
    line->setLineWidth(2);

    // 右侧说明区
    QVBoxLayout *descLayout = new QVBoxLayout();
    QLabel *descLabel = new QLabel("欢迎使用柜控", window);
    descLabel->setAlignment(Qt::AlignCenter);
    descLabel->setStyleSheet("font-size: 40px; font-weight: bold; color: #0099cc;");
    descLayout->addStretch();
    descLayout->addWidget(descLabel);
    descLayout->addStretch();

    // 组装左右布局
    bodyLayout->addLayout(btnLayout, 1);
    bodyLayout->addWidget(line);
    bodyLayout->addLayout(descLayout, 2);

    mainLayout->addLayout(bodyLayout);

    // 信号连接
    QObject::connect(adminBtn, &QPushButton::clicked, [board, window]()
                     { board->handleAdmin(window); });
    QObject::connect(storeBtn, &QPushButton::clicked, [board, window]()
                     { board->handleStore(window); });
    QObject::connect(pickupBtn, &QPushButton::clicked, [board, window]()
                     { board->handleHalfway(window); });
    QObject::connect(releaseBtn, &QPushButton::clicked, [board, window]()
                     { board->handleReturn(window); });
    QObject::connect(longRentBtn, &QPushButton::clicked, [board, window]()
                     { board->handleLongRent(window); }); // 新增信号
}

bool Board::processUI(int argc, char *argv[])
{
    QApplication app(argc, argv);
    // AeaQt::KeyboardManager::getInstance().preloadNumberKeyboard();
    AeaQt::KeyboardManager::getInstance().preloadKeyboard();

    QWidget window;
    window.setWindowTitle("全屏主页面");
    window.showFullScreen();

    setupMainUI(this, &window);

    return app.exec();
}